package datastructure.ch04_stacks.ch04_linked_stack;

import datastructure.ch03_linked.ch09_single_linkedlist.SingleLinkedList;
import datastructure.ch04_stacks.ch02_interface_abstarct.Stack;

/**
 *
 *
 * @author guodd
 * @version 1.0
 */
public class LinkedStack<E> implements Stack<E> {
    public static void main(String[] args) {
        LinkedStack<Object> data = new LinkedStack<>();
        data.push(1);
        data.push(2);
        data.push(3);
        data.push(4);

        System.out.println(data.pop());
        System.out.println(data.peek());
        System.out.println(data.getSize());
    }

    SingleLinkedList<E> linkedList;

    public LinkedStack() {
        linkedList = new SingleLinkedList<>();
    }

    @Override
    public void push(E e) {
        linkedList.add(e);
    }

    @Override
    public E pop() {
        return linkedList.remove(getSize() - 1);
    }

    @Override
    public E peek() {
        return linkedList.get(getSize() - 1);
    }

    @Override
    public boolean isEmpty() {
        return linkedList.isEmpty();
    }

    @Override
    public void clear() {
        linkedList.clear();
    }

    @Override
    public int getSize() {
        return linkedList.size();
    }
}
